package com.ybwh.springboot2.controller;

import com.ybwh.springboot2.common.RespData;
import com.ybwh.springboot2.dao.AreaMapper;
import com.ybwh.springboot2.mapstruct.UserInfo;
import com.ybwh.springboot2.model.vo.AreaVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author saytime
 * @since 2018-12-06
 */
@Api(tags="地区")
@Controller
@RequestMapping("/area")
public class AreaController {

    @Autowired
    private AreaMapper areaMapper;

    @ResponseBody
    @RequestMapping(value = "/validate", method = RequestMethod.GET)
    public RespData<Object> validate(@Validated @ModelAttribute @RequestBody AreaVo areaVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (ObjectError error : bindingResult.getAllErrors()) {
                return RespData.failedResp(error.getDefaultMessage());
            }
        }

        return RespData.successResp(null);
    }

    @ResponseBody
    @RequestMapping(value = "/validate2", method = RequestMethod.GET)
    public RespData<Object> validate2(@ApiParam(value = "参数data2") @RequestParam("data2") String data) {


        return RespData.successResp(null);
    }


    @ResponseBody
    @RequestMapping(value = "/getArea1", method = RequestMethod.GET)
    public RespData<Object> getArea(@ApiParam(value = "id") @RequestParam("id") Long id, UserInfo userInfo) {

        System.out.println(userInfo);

        return RespData.successResp(areaMapper.selectById(id));
    }


    @ResponseBody
    @RequestMapping(value = "/getArea", method = RequestMethod.GET)
    public RespData<Object> getArea(@ApiParam(value = "id") @RequestParam("id") Long id) {

        return RespData.successResp(areaMapper.selectById(id));
    }


    @ApiOperation(value = "上传文件", notes = "上传文件")
    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public RespData upload(@ApiParam(value="上传文件",required = true) @RequestParam("file") MultipartFile file)
            throws IllegalStateException, IOException {


        file.transferTo(new File("D:/tmp/" + file.getOriginalFilename()));


        return RespData.successResp(null);

    }


}
